package shop;

import commodity.Commodity;
import java.io.*;
import java.math.BigDecimal;
import java.util.HashMap;

public class Mall {
    private static HashMap<Commodity,Integer> mallHashMap=new HashMap<>();   //哈希表存储商品，Integer为库存

    public static HashMap<Commodity, Integer> getMallHashMap() {
        return mallHashMap;
    }

    //读入商城商品
    public static void read()
    {
        mallHashMap.clear();
       // BufferedReader br = null;
        try {
            String path = "resource/Commodity.txt";//Mall.class.getClassLoader().getResource("Commodity.txt").getPath();

//            path = path.replace("file:/", "");
//            path = path.replace("/", "//");
            File file=new File(path);
            FileReader fr=new FileReader(file.getAbsoluteFile());
            BufferedReader br = new BufferedReader(fr);
           // br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
            String data = null;
            while(true){
                try {
                    if (((data = br.readLine())==null)) {
                        br.close();
                        return;
                    }
                    String dataSplit[] = data.split("\\s+");
                    Commodity commodity=new Commodity(dataSplit[0], new BigDecimal(dataSplit[1]));
                    mallHashMap.put(commodity,Integer.parseInt(dataSplit[2]));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }


    //写入文件
    public static void writeCommodity()
    {
        try {
            String path = "resource/Commodity.txt";
            File file=new File(path);
            FileWriter fw = new FileWriter(file.getAbsoluteFile());//file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            for(Commodity commodity:mallHashMap.keySet()){
                String data=commodity.getName()+" "+Main.df.format(commodity.getPrice())+" "+mallHashMap.get(commodity);
                bw.write(data+"\n");
            }
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    //是否存在商品，有则返回该商品
    public static Commodity isExistCommodity(String commodityName)
    {
        //read();
        for (Commodity commodity:mallHashMap.keySet()) {
            if(commodity.getName().equals(commodityName)){
                //System.out.println(commodity);
                return commodity;
            }
        }
        return null;
    }



    //判断商品库存
    public static boolean isOutOfStock(String commodityName, int num)
    {
        //read();
        Commodity commodity=isExistCommodity(commodityName);
        if(num>mallHashMap.get(commodity)) {
            return true;
        } else {
            return false;
        }
    }


    public static void setMallHashMap(String commodityName, int num) {
        Commodity commodity=isExistCommodity(commodityName);
        mallHashMap.merge(commodity,-1*num,Integer::sum);

    }

    //搜索商品并输出
    public void searchCommodity()
    {
        System.out.println("请输入查找的商品名称");
        String commodityName=Main.scanner.nextLine();
        Commodity commodity=isExistCommodity(commodityName);
        if(commodity!=null)
        {
            System.out.println("商品名称："+commodity.getName()+" 商品单价："+Main.df.format(commodity.getPrice())
                    +" 商品库存："+mallHashMap.get(commodity));
        }else {
            System.out.println("不存在该商品");
        }
    }


    //添加商品
    public void putOnTheShelf()
    {
        //read();
        System.out.println("请输入商品名称，价格，及库存");
        String data=Main.scanner.nextLine();
        String[] dataSplit = data.split(" ");
        Commodity commodityIsExist=isExistCommodity(dataSplit[0]);
        if(commodityIsExist!=null) {
            mallHashMap.merge(commodityIsExist,Integer.parseInt(dataSplit[2]),Integer::sum);
        } else {
            Commodity commodity=new Commodity(dataSplit[0],BigDecimal.valueOf(Long.parseLong(dataSplit[1])));
            mallHashMap.put(commodity, Integer.valueOf(dataSplit[2]));
        }
        //merge()它将新的值赋值到 key (如果不存在)或更新给定的key 值对应的 value,最后的是remappingFunction，返回参数总和

        //writeCommodity();
    }



    //下架商品
    public void offShelf()
    {
        //read();
        System.out.println("请输入下架商品名称");
        String commodityName=Main.scanner.nextLine();
        Commodity commodity= isExistCommodity(commodityName);
        if(commodity==null){
            System.out.println("找不到该商品！");
        }else {
            mallHashMap.remove(commodity);
            System.out.println("下架成功！");
        }

        //writeCommodity();
    }




    //展示商城
    public static void showMall()
    {
        //read();
        for(Commodity commodity:mallHashMap.keySet()){
            System.out.println("商品名称："+commodity.getName()+" 商品单价："+Main.df.format(commodity.getPrice())
                    +" 商品库存："+mallHashMap.get(commodity));
        }
    }



}
